package com.tang.multi.v1;

import lombok.SneakyThrows;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

/**
 * @author Tang
 * @classname Process
 * @description [  ]
 * @date 2021/8/10 16:05
 */
public class Process implements Runnable {

    private ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

    private SelectionKey selectionKey;

    private SocketChannel socketChannel;

    public Process(SelectionKey selectionKey, SocketChannel socketChannel) {
        this.selectionKey = selectionKey;
        this.socketChannel = socketChannel;
    }

    @SneakyThrows
    @Override
    public void run() {
        int len = this.socketChannel.read(byteBuffer);
        byteBuffer.flip();
        System.out.println(Thread.currentThread().getName() + "-receive msg:--->" + new String(byteBuffer.array(), 0, len));
        byteBuffer.clear();
    }
}